package com.guaoran.micro.services.spring.cloud.stream.kafka.stream.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @author : gucheng
 * @Description : <br/>
 *  消息生产者
 * @date :2018/12/7 12:17
 */
@Component
@EnableBinding({Source.class})
public class MessageProducerBean {
    @Autowired
    @Qualifier(Source.OUTPUT) // Bean 名称
    private MessageChannel messageChannel;
    @Autowired
    private Source source;
    /**
     * 发送消息
     * @param message
     */
    public void send(String message){
        //通过消息管道发送消息.两种方式都可以
//        messageChannel.send(MessageBuilder.withPayload(message).build());
        source.output().send(MessageBuilder.withPayload(message).build());
    }
}
